/**
 * 战斗修复测试
 * 验证敌人创建和战斗系统是否正常工作
 */

import enemy from '../entity/enemy';
import player from '../entity/player';
import { ComType } from '../../tool/component';
import rpg_component from '../../../component/rpg_component';
import { config } from '../../../config';

/**
 * 测试敌人创建
 */
function testEnemyCreation() {
    console.log('=== 测试敌人创建 ===');
    
    try {
        // 创建敌人
        const testEnemy = new enemy();
        testEnemy.randomConfig('test');
        
        // 检查敌人组件
        const rpgCom = testEnemy.getComponent(ComType.rpg) as rpg_component;
        const baseCom = testEnemy.getComponent(ComType.base) as any;
        
        console.log(`敌人ID: ${testEnemy.id}`);
        console.log(`敌人名称: ${baseCom.nickName}`);
        console.log(`敌人表情: ${baseCom.emoji}`);
        console.log(`敌人生命值: ${rpgCom.hp_now}/${(rpgCom as any).hp_max || rpgCom.hp_now}`);
        console.log(`敌人攻击力: ${rpgCom.getAttribute('物理攻击')}`);
        console.log(`敌人技能数量: ${(rpgCom as any).skills?.length || 0}`);
        
        if ((rpgCom as any).skills?.length > 0) {
            console.log('敌人技能:', (rpgCom as any).skills.map((s: any) => s.skillId).join(', '));
        }
        
        console.log('✅ 敌人创建测试通过');
        return testEnemy;
        
    } catch (error) {
        console.error('❌ 敌人创建测试失败:', error);
        return null;
    }
}

/**
 * 测试战斗创建
 */
function testBattleCreation() {
    console.log('\n=== 测试战斗创建 ===');
    
    try {
        // 创建玩家
        const testPlayer = new player();
        testPlayer.getComponent(ComType.血统).addTalent('2');
        const playerRpg = testPlayer.getComponent(ComType.rpg) as rpg_component;
        playerRpg.addSk({ skillId: 'A000' }); // 普通攻击
        
        // 创建敌人
        const testEnemy = new enemy();
        testEnemy.randomConfig('test');
        const enemyRpg = testEnemy.getComponent(ComType.rpg) as rpg_component;
        
        console.log(`玩家生命值: ${playerRpg.hp_now}/${(playerRpg as any).hp_max || playerRpg.hp_now}`);
        console.log(`玩家攻击力: ${playerRpg.getAttribute('物理攻击')}`);
        console.log(`玩家技能数量: ${(playerRpg as any).skills?.length || 0}`);
        
        console.log(`敌人生命值: ${enemyRpg.hp_now}/${(enemyRpg as any).hp_max || enemyRpg.hp_now}`);
        console.log(`敌人攻击力: ${enemyRpg.getAttribute('物理攻击')}`);
        console.log(`敌人技能数量: ${(enemyRpg as any).skills?.length || 0}`);
        
        // 检查双方是否都有技能
        if ((playerRpg as any).skills?.length === 0) {
            console.error('❌ 玩家没有技能');
            return false;
        }
        
        if ((enemyRpg as any).skills?.length === 0) {
            console.error('❌ 敌人没有技能');
            return false;
        }
        
        console.log('✅ 战斗创建测试通过');
        return true;
        
    } catch (error) {
        console.error('❌ 战斗创建测试失败:', error);
        return false;
    }
}

/**
 * 测试配置加载
 */
function testConfigLoading() {
    console.log('\n=== 测试配置加载 ===');
    
    try {
        // 测试敌人配置
        const enemyConfig = config.get('enemy_test') as Map<string, any>;
        if (!enemyConfig) {
            console.error('❌ 无法加载敌人配置');
            return false;
        }
        
        console.log(`敌人配置数量: ${enemyConfig.size}`);
        
        // 检查第一个配置
        const keys = Array.from(enemyConfig.keys());
        if (keys.length > 0) {
            const firstKey = keys[0];
            const firstConfig = enemyConfig.get(firstKey);
            
            console.log(`第一个配置ID: ${firstConfig.id}`);
            console.log(`第一个配置名称: ${firstConfig.name}`);
            console.log(`第一个配置技能数量: ${firstConfig.skill ? firstConfig.skill.length : 0}`);
            
            if (firstConfig.skill && firstConfig.skill.length > 0) {
                console.log('技能配置:', firstConfig.skill);
            }
        }
        
        console.log('✅ 配置加载测试通过');
        return true;
        
    } catch (error) {
        console.error('❌ 配置加载测试失败:', error);
        return false;
    }
}

/**
 * 运行所有测试
 */
function runAllTests() {
    console.log('开始战斗修复测试...\n');
    
    const results = [
        testConfigLoading(),
        testEnemyCreation(),
        testBattleCreation()
    ];
    
    const passedTests = results.filter(r => r).length;
    const totalTests = results.length;
    
    console.log(`\n=== 测试结果 ===`);
    console.log(`通过测试: ${passedTests}/${totalTests}`);
    
    if (passedTests === totalTests) {
        console.log('🎉 所有测试通过！战斗系统修复成功！');
        console.log('✅ 敌人现在有技能了！');
        console.log('✅ 战斗应该能正常进行了！');
    } else {
        console.log('❌ 部分测试失败，需要进一步检查');
    }
}

// 运行测试
runAllTests(); 